Skip to main content
Jørgen Borgesen

Play random sounds in Home Assistant

With Halloween just around the corner, now is the time to add spooky sounds to your smart home. In this post we will look at setting up an automation in Home Assistant to pick a random sound and play it on a speaker.

Adding sound clips to your Home Assistant #

First of all we need some sound clips. You can search the internet or Github to find some that you like.

The next step is to add these files to your Home Assistant instance. Visit the media browser in your Home Assistant instance from the sidebar or by following the link. Tap the source My Media and then tap Manage in the upper right corner. This pops open a dialog, with a button to add media at the top. Once you have added your sound clips, you can close the dialog and play the sound clips to see that everything works as expected. You can play them on your favorite speaker by switching the output in the bottom right corner.

Setting up the automation #

Navigate to your Automations and create a blank automation.

First, lets add a trigger. I'm using a wireless button as a trigger, but you can use anything you like. Next, lets add an action. Click + ADD ACTION and select _Play media. Then select your speaker, and one of your scary sounds. The result looks like this is the 2023.10 version:

Automation

Save the automation and test that it is working before we move on to play a random sound.

To play a random song we are going to add some template code to our automation. To do that we need to switch to YAML mode for our automation. The triple dot menu of the automation allows you to switch to YAML mode:

Automation menu

Click Edit in YAML to open an inline editor to change the YAML configuration. The initial code in my automation looks like this:

service: media_player.play_media
target:
  entity_id: media_player.stua
data:
  media_content_id: media-source://media_source/local/Scary.mp3
  media_content_type: audio/mpeg
metadata:
  title: Scary.mp3
  thumbnail: null
  media_class: music
  children_media_class: null
  navigateIds:
    - {}
    - media_content_type: app
      media_content_id: media-source://media_source

The important line is the one specifying the media_content_id. It specifies which file to play. In my case it Scary.mp3. We are only going to edit the media_content_id property, but I'll include the complete YAML for completeness and easy copy paste.

All the sounds I want to play are all in the same location. Home assistant uses the Jinja templating language for its template needs. My solution adds an array with all the sounds I want to play, then pipes that through the random operator to pick a random one. The result looks like this:

service: media_player.play_media
target:
  entity_id: media_player.stua
data:
  media_content_id: |
    {{- [
      "media-source://media_source/local/Evil_Laugh.mp3",
      "media-source://media_source/local/Scary.mp3",
      "media-source://media_source/local/Evil.mp3",
      "media-source://media_source/local/Scream.mp3",
      "media-source://media_source/local/Spooky.mp3",
      "media-source://media_source/local/Haunted.mp3"
      ] | random -}}
  media_content_type: audio/mpeg
metadata:
  title: Scary.mp3
  thumbnail: null
  media_class: music
  children_media_class: null
  navigateIds:
    - {}
    - media_content_type: app
      media_content_id: media-source://media_source

All that is left is to save the automation and test that it works.

Bonus: Add additional speakers #

If you have multiple speakers around the house you can play a random sound on each of them for a more spooky effect. Playing different sounds on multiple speakers might not generally be a great idea, but for works really well for scary halloween sounds.